"""
简单的substr 函数替换
"""

import re
import logging


def change_simple_substr_condition_to_substring(m):
    new_substring_fn = "SUBSTRING({},{},{})".format(str(m.group(2)).strip(), str(m.group(3)).strip(), str(m.group(4)).strip())
    return new_substring_fn


def rep_simple_fn_substr(document_content):
    logging.info(" 》》》 开始执行简单 substr 替换 ")
    # substr('1|2|3',1,4)

    unix_timestamp_patten = re.compile(r"(substr|SUBSTR)\(([^,\(\)]*),([^,\(\)]*),([^,\(\)]*)\)")
    result = unix_timestamp_patten.sub(change_simple_substr_condition_to_substring, document_content)
    document_content = result
    return document_content
